package com.du.config;


import java.lang.reflect.Field;
import java.util.Map;
import java.util.TreeMap;

/**
 * @Desc : mysql匹配规则
 * @Author: shuangdu
 * @date : 2021/3/26
 */
public class MysqlRule {

    private final RuleInfo regexMap = new RuleInfo();

    public static final String KEY_SPLIT = "`,`";

    public static final String COL_WRAP_REGEX = "[ `]*";

    public static final String TABLE_NAME_PREFIX = "TABLE[ ]*";
    public static final String TABLE_TYPE_PREFIX = "ENGINE[ ]*=";
    public static final String TABLE_CHARSET_PREFIX = "CHARSET[ ]*=";
    public static final String TABLE_DESC_PREFIX = "COMMENT[ ]*=";

    public static final Map<String, String> COL_MAP = new TreeMap<>();

    public static final String DEFAULT_TEMPLATE_RULE = "|@1|@2|@2-3|@2-4|@3|@4|";

    static {
        Class<RuleInfo> ruleInfoClass = RuleInfo.class;
        Field[] fields = ruleInfoClass.getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(MapInfo.class)) {
                MapInfo mapInfo = field.getAnnotation(MapInfo.class);
                COL_MAP.put(mapInfo.sign(), mapInfo.text());
            }
        }
    }

    public MysqlRule() {

        regexMap.setTableName("TABLE[ ]*`.*`[ ]*[(]");

        regexMap.setColumnName("^[ ]*`.*`");

        regexMap.setColumnType("([a-z]*[(][0-9, ]*[)])|(float)|(double)|(mediumtext)|(text)|(blob)|(longtext)|(longblob)|(datetime)|(timestamp)");

        regexMap.setColumnLength("[(][0-9, ]*[)]");

        regexMap.setColumnIsNull("[ ]*not[ ]*null[ ]*");

        regexMap.setColumnDefault("[ ]*default[ ]*(null|(current_timestamp)|([',.0-9a-zA-Z\\u4e00-\\u9fa5]*))");

        regexMap.setColumnComment("comment[ ]*'.*'");

        regexMap.setPrimaryKey("^[ ]*primary[ ]*key[ ]*[(][`a-z_, ]*[)]");

        regexMap.setUniqueKey("^[ ]*unique[ ]*key[ ]*[(][`a-z_, ]*[)]");

        regexMap.setIndexKey("^[ ]*key[ ]*`.*`[ ]*[(][`a-z_, ]*[)]");

        // table引擎
        regexMap.setTableType("ENGINE[ ]*=[ ]*[a-zA-Z]*[ ]*");

        regexMap.setTableCharset("CHARSET[ ]*=[ ]*[a-zA-Z0-9]*[ ]*");

        regexMap.setTableDesc("COMMENT[ ]*=[ ]*.*'");

    }

    public final RuleInfo ruleInfo() {
        return this.regexMap;
    }



    public static class RuleInfo {

        private String tableName;

        private String tableType;

        private String tableDesc;

        private String tableCharset;

        @MapInfo(sign = "@1", text = "字段名称")
        private String columnName;

        @MapInfo(sign = "@2", text = "字段类型")
        private String columnType;

        @MapInfo(sign = "@2-1", text = "字段类型名称")
        private String columnTypeName;

        @MapInfo(sign = "@2-2", text = "字段长度")
        private String columnLength;

        @MapInfo(sign = "@2-3", text = "字段能否为空")
        private String columnIsNull;

        @MapInfo(sign = "@2-4", text = "字段默认值")
        private String columnDefault;

        @MapInfo(sign = "@3", text = "字段描述")
        private String columnComment;

        @MapInfo(sign = "@4", text = "主键 or 索引")
        private String primaryKey;

        @MapInfo(sign = "@4", text = "主键 or 索引")
        private String uniqueKey;

        @MapInfo(sign = "@4", text = "主键 or 索引")
        private String indexKey;

        @MapInfo(sign = "@5", text = "字段java类型")
        private String columnJavaTypeSimpleName;

        public String getTableName() {
            return tableName;
        }

        public void setTableName(String tableName) {
            this.tableName = tableName;
        }

        public String getTableType() {
            return tableType;
        }

        public void setTableType(String tableType) {
            this.tableType = tableType;
        }

        public String getTableDesc() {
            return tableDesc;
        }

        public void setTableDesc(String tableDesc) {
            this.tableDesc = tableDesc;
        }

        public String getTableCharset() {
            return tableCharset;
        }

        public void setTableCharset(String tableCharset) {
            this.tableCharset = tableCharset;
        }

        public String getColumnName() {
            return columnName;
        }

        public void setColumnName(String columnName) {
            this.columnName = columnName;
        }

        public String getColumnType() {
            return columnType;
        }

        public void setColumnType(String columnType) {
            this.columnType = columnType;
        }

        public String getColumnTypeName() {
            return columnTypeName;
        }

        public void setColumnTypeName(String columnTypeName) {
            this.columnTypeName = columnTypeName;
        }

        public String getColumnLength() {
            return columnLength;
        }

        public void setColumnLength(String columnLength) {
            this.columnLength = columnLength;
        }

        public String getColumnIsNull() {
            return columnIsNull;
        }

        public void setColumnIsNull(String columnIsNull) {
            this.columnIsNull = columnIsNull;
        }

        public String getColumnDefault() {
            return columnDefault;
        }

        public void setColumnDefault(String columnDefault) {
            this.columnDefault = columnDefault;
        }

        public String getColumnComment() {
            return columnComment;
        }

        public void setColumnComment(String columnComment) {
            this.columnComment = columnComment;
        }

        public String getPrimaryKey() {
            return primaryKey;
        }

        public void setPrimaryKey(String primaryKey) {
            this.primaryKey = primaryKey;
        }

        public String getUniqueKey() {
            return uniqueKey;
        }

        public void setUniqueKey(String uniqueKey) {
            this.uniqueKey = uniqueKey;
        }

        public String getIndexKey() {
            return indexKey;
        }

        public void setIndexKey(String indexKey) {
            this.indexKey = indexKey;
        }

        public String getColumnJavaTypeSimpleName() {
            return columnJavaTypeSimpleName;
        }

        public void setColumnJavaTypeSimpleName(String columnJavaTypeSimpleName) {
            this.columnJavaTypeSimpleName = columnJavaTypeSimpleName;
        }
    }
}
